package com.tul.exercise.base.swing.components;

import com.tul.exercise.base.swing.SwingConsole;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * @Description 《java编程思想》第22章 示例14：
 * <p>
 * Icom：
 **/
public class Faces extends JFrame {
    private static Icon[] faces;
    private JButton jb = new JButton("Disable");
    private JButton jb2 = new JButton("Disable");
    private boolean mad = false;

    public Faces() {
        faces = new Icon[]{
                new ImageIcon(getClass().getResource("/icons/Face0.gif")),
                new ImageIcon(getClass().getResource("/icons/Face1.gif")),
                new ImageIcon(getClass().getResource("/icons/Face2.gif")),
                new ImageIcon(getClass().getResource("/icons/Face3.gif")),
                new ImageIcon(getClass().getResource("/icons/Face4.gif"))
        };

        jb = new JButton("JButton", faces[3]);

        setLayout(new FlowLayout());

        jb.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (mad) {
                    jb.setIcon(faces[3]);
                } else {
                    jb.setIcon(faces[0]);
                    mad = true;
                }
                jb.setVerticalAlignment(JButton.TOP);
                jb.setHorizontalAlignment(JButton.LEFT);
            }
        });

        jb.setRolloverEnabled(true);
        // jb 浮动的icon
        jb.setRolloverIcon(faces[1]);
        // jb Disable状态的 icon
        jb.setDisabledIcon(faces[4]);

        // 设置“工具提示”见22.8.3章节
        jb.setToolTipText("Yow!");

        add(jb);

        // 设置jb的状态
        jb2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (jb.isEnabled()) {
                    jb.setEnabled(false);
                    jb2.setText("Enable");
                } else {
                    jb.setEnabled(true);
                    jb2.setText("Disable");
                }
            }
        });
        add(jb2);
    }

    public static void main(String[] args) {
        SwingConsole.run(new Faces(), 250, 125);
    }
}
